package com.his.struts.dao.note;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.his.struts.dao.medicine.MedicineImp;
import com.his.struts.dao.users.UsersImp;
import com.his.struts.form.NoteForm;
import com.his.struts.util.DbUtil;

/**
 * 日志实现类
 * 
 * @author liuyan
 * 
 */
public class NoteImp implements NoteDao {
	Logger log = Logger.getLogger(NoteImp.class);
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	 DbUtil db = new DbUtil();

	/**
	 * 用户所有日记
	 * 
	 * @param rs
	 * @return List<NoteForm> 日志集合
	 */
	public List<NoteForm> allNote(ResultSet rs) {
		List<NoteForm> list = new ArrayList<NoteForm>();
		try {
			while (rs.next()) {
				NoteForm nt = new NoteForm();
				nt.setNb_id(rs.getInt("nb_id"));
				nt.setNb_title(rs.getString("nb_title"));
				nt.setNb_turn(rs.getString("nb_turn"));
				nt.setNb_type(rs.getInt("nb_type"));
				nt.setNb_img(rs.getString("nb_img"));
				nt.setRead_number(rs.getInt("read_number"));
				nt.setResponse_number(rs.getInt("response_number"));
				nt.setNb_time(rs.getDate("nb_time"));
				nt.setTestnote(rs.getString("testnote"));
				nt.setNb_lock(rs.getString("nb_lock"));
				nt.setPublish(finduname(rs.getInt("d_id"), rs.getInt("u_id")));
				list.add(nt);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	/**
	 * 用户日记大小
	 * 
	 * @param uid
	 *            用户id
	 * @return int 数量
	 */
	public int allNoteNum(int uid) {
		ArrayList list = new ArrayList();
		String sql = "select count(*) from notebook where u_id = ?";
		list.add(uid);
		ResultSet rs = db.executeQuery(sql, list);
		int i = 0;
		try {
			while (rs.next()) {
				i = rs.getInt(1);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return i;
	}

	/**
	 * 查看日记
	 * 
	 * @param uid
	 *            用户id
	 * @return NoteForm日志form
	 */
	public NoteForm showNote(int uid) {
		ArrayList list = new ArrayList();
		String sql = "select testnote,nb_title from notebook where nb_id = ?";
		list.add(uid);
		ResultSet rs = db.executeQuery(sql, list);
		NoteForm nt = new NoteForm();
		try {
			while (rs.next()) {
				nt.setTestnote(rs.getString(1));
				nt.setNb_title(rs.getString(2));
				nt.setNb_id(uid);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return nt;
	}

	/**
	 * 用户编辑日记
	 * 
	 * @param nf
	 *            日志form
	 * @return boolean 是否编辑成功
	 */
	public boolean editNote(NoteForm nf) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		ArrayList list2 = new ArrayList();
		String sql = "insert into notebook (u_id,nb_title,nb_type,nb_time,nb_lock,testnote) values (?,?,?,?,?,?)";
		Date date = new Date();
		String time = sdf.format(date);
		list.add(nf.getU_id());
		list.add(nf.getNb_title());
		list.add(nf.getNb_type());
		list.add(time);
		list.add(2);
		list.add(nf.getTestnote());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 医生所有日记
	 * 
	 * @param did
	 *            医生id
	 * @return List<NoteForm>日志集合
	 */
	public List<NoteForm> allDocNote(int did) {
		List<NoteForm> list = new ArrayList<NoteForm>();
		ArrayList list1 = new ArrayList();
		String sql = "select * from notebook where d_id = ?";
		list1.add(did);
		ResultSet rs = db.executeQuery(sql, list1);
		try {
			while (rs.next()) {
				NoteForm nt = new NoteForm();
				nt.setNb_id(rs.getInt("nb_id"));
				nt.setNb_title(rs.getString("nb_title"));
				nt.setNb_turn(rs.getString("nb_turn"));
				nt.setNb_type(rs.getInt("nb_type"));
				nt.setNb_img(rs.getString("nb_img"));
				nt.setRead_number(rs.getInt("read_number"));
				nt.setResponse_number(rs.getInt("response_number"));
				nt.setNb_time(rs.getDate("nb_time"));
				nt.setTestnote(rs.getString("testnote"));
				nt.setNb_lock(rs.getString("nb_lock"));
				list.add(nt);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	/**
	 * 医生日记大小
	 * 
	 * @param did
	 *            医生id
	 * @return int
	 */
	public int allDocNoteNum(int did) {
		ArrayList list = new ArrayList();
		String sql = "select count(*) from notebook where d_id = ?";
		list.add(did);
		ResultSet rs = db.executeQuery(sql, list);
		int i = 0;
		try {
			if(rs != null){
			while (rs.next()) {
				i = rs.getInt(1);
			}
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return i;
	}

	/**
	 * 医生编辑日记
	 * 
	 * @param nf
	 *            日志form
	 * @return boolean 是否编辑成功
	 */
	public boolean docEditNote(NoteForm nf) {
		boolean ok = false;
		ArrayList list = new ArrayList();
		ArrayList list2 = new ArrayList();
		String sql = "insert into notebook (d_id,nb_title,nb_type,nb_time,nb_lock,testnote) values (?,?,?,?,?,?)";
		Date date = new Date();
		String time = sdf.format(date);
		list.add(nf.getDocnb_id());
		list.add(nf.getNb_title());
		list.add(nf.getNb_type());
		list.add(time);
		list.add(2);
		list.add(nf.getTestnote());
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}

	/**
	 * 管理员查看日记
	 * 
	 * @return List<NoteForm> 日志集合
	 */
	public List<NoteForm> adminNote() {
		List<NoteForm> list = new ArrayList<NoteForm>();
		String sql = "select * from notebook";
		ResultSet rs = db.executeQuery(sql, null);
		try {
			while (rs.next()) {
				NoteForm nt = new NoteForm();
				nt.setNb_id(rs.getInt("nb_id"));
				nt.setNb_title(rs.getString("nb_title"));
				nt.setNb_turn(rs.getString("nb_turn"));
				nt.setNb_type(rs.getInt("nb_type"));
				nt.setNb_img(rs.getString("nb_img"));
				nt.setRead_number(rs.getInt("read_number"));
				nt.setResponse_number(rs.getInt("response_number"));
				nt.setNb_time(rs.getDate("nb_time"));
				nt.setTestnote(rs.getString("testnote"));
				nt.setNb_lock(rs.getString("nb_lock"));
				nt.setPublish(finduname(rs.getInt("d_id"), rs.getInt("u_id")));
				list.add(nt);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}

	/**
	 * 查找uname
	 * @param uid 用户id
	 * @param did 医生id
	 * @return String name
	 */
	public String finduname(int uid, int did) {
		Logger log = Logger.getLogger(NoteImp.class);
		ArrayList list = new ArrayList();
		ArrayList list2 = new ArrayList();
		String uname = null;
		ResultSet rs;
		if (did == 0) {
			String sql = "select uname from userinfo where uid = ?";
			list.add(uid);
			rs = db.executeQuery(sql, list);
		} else {
			String sql = "select dname from doctorinfo where did = ?";
			list2.add(did);
			rs = db.executeQuery(sql, list2);
		}
		try {
			while (rs.next()) {
				uname = rs.getString(1);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return uname;
	}

	/**
	 * 删除日记
	 * 
	 * @param nbid
	 * @return 数量
	 */
	public boolean delNote(int nbid) {
		ArrayList list = new ArrayList();
		boolean ok = false;
		String sql = "delete from notebook where nb_id = ?";
		list.add(nbid);
		int i = db.executeUpdate(sql, list);
		if (i != 0) {
			ok = true;
		}
		db.closeAll();
		return ok;
	}
	
	/**
	 * 封装bean
	 * @param rs 所有日志
	 * @return
	 */
	public List<NoteForm> getBean(ResultSet rs) {
		List<NoteForm> list = new ArrayList<NoteForm>();
		try {
			while (rs.next()) {
				NoteForm nt = new NoteForm();
				nt.setNb_id(rs.getInt("nb_id"));
				nt.setNb_title(rs.getString("nb_title"));
				nt.setNb_turn(rs.getString("nb_turn"));
				nt.setNb_type(rs.getInt("nb_type"));
				nt.setNb_img(rs.getString("nb_img"));
				nt.setRead_number(rs.getInt("read_number"));
				nt.setResponse_number(rs.getInt("response_number"));
				nt.setNb_time(rs.getDate("nb_time"));
				nt.setTestnote(rs.getString("testnote"));
				nt.setNb_lock(rs.getString("nb_lock"));
				list.add(nt);
			}
		} catch (SQLException e) {
			log.error(e.getMessage());
		} finally {
			db.closeAll();
		}
		return list;
	}
}
